import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const { VITE_BASE_URL } = loadEnv(mode, root)

  console.log('command', command)

  return {
    base: VITE_BASE_URL,
    // base: '/vue3-store-admin',
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, './src'),
        },
      ],
    },
    plugins: [
      vue(),
      styleImport({
        libs: [
          {
            libraryName: 'vant',
            esModule: true,
            resolveStyle: (name) => `vant/es/${name}/style`,
          },
        ],
      }),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/_variables.scss", "@/styles/_mixins.scss";`,
        },
        less: {
          modifyVars: {},
          javascriptEnabled: true,
        },
      },
    },
    server: {
      port: 2000,
      // proxy: {
      //   '/api': {
      //     target: 'http://localhost:3000/',
      //     changeOrigin: true,
      //     rewrite: (path) => path.replace(/^\/api/, '/api/'),
      //   },
      // },
    },
  }
}
